<?php
require 'includes/config.php';
require 'includes/auth.php';
require 'includes/sanitize.php';

function addToList($obj,$backend){
	// Make request to backend, resolve the owers usedid to username
	$request = new makeRequest($backend.'api.php?method=user&id='.$obj->{'owner'}, 'GET');
	$request = $request->makeRequest();
	$rbody = $request[0];
	$http_code = $request [1];
	$userobj = json_decode($rbody);
	// Resolve the securitylevel
	switch ($obj->{'securitylevel'}){
		case 1:
			$level = "Open for all";
			break;
		case 2:
			$level = "Friends only";
			break;
		case 3:
			$level = "Invite only";
			break;
	}
	// Make a row with the data
	$res="<tr>
		<td>".$obj->{'name'}."</td>
		<td>".$obj->{'description'}."</td>
		<td>".$obj->{'draftlimit'}."</td>
		<td>".$obj->{'moneylimit'}."</td>
		<td>".$obj->{'playerlimit'}."</td>
		<td>".$userobj[0]->{'username'}."</td>
		<td>".$level."</td>
		<td><a href='editleague.php?id=".$obj->{'id'}."'>Edit</a></td></tr>";
	return $res;
}
sanitize($_GET);
// The begginning of the table
$msg.="<table border='1'><tr>
<th>League name</th>
<th>League description</th>
<th>Draft limit</th>
<th>Money limit</td>
<th>Player limit</th>
<th>League owner</th>
<th>League opento</th>
<th>Edit</th></tr>";	
$pagelimit = 10;
if(isset($_GET['id'])){
	$request = new makeRequest($backend.'api.php?method=league&auid='.$_SESSION['uid'], 'GET');
	$tmp = $request->makeRequest();
	$rbody = $tmp[0];
	$obj = json_decode($rbody);
	$http_code = $tmp[1];
	if($http_code ==200){
		$nrofleagues = 1; // this need to be fixed
	}
}else{
// List all leagues
	$request = new makeRequest($backend.'api.php?method=league&count=true', 'GET'); // should return number of leagues
	$tmp = $request->makeRequest();
	$rbody = $tmp[0];
	$http_code = $tmp[1];
	$obj = json_decode($rbody);
	$nrofleagues = $obj->count;
}
// Check that there exist some leagues
// Check for errors
if($http_code == 200){
	if($nrofleagues == "0"){
		$msg="Error, no leagues found";
	}else{
		// If user ask for specific league
		if(isset($_GET['id'])){
			$request = new makeRequest($backend.'api.php?method=league&id='.$data['id'], 'GET');
			$tmp = $request->makeRequest();
			$rbody = $tmp[0];
			$obj = json_decode($rbody);
			$http_code = $tmp[1];
			// Check so there is no errors
			if($http_code == 200){
				$msg.= addToList($obj[0],$backend);
				$msg.="</table>";
			}
			// If no league name specified, show all leagues
		}else{
			// Check how many pages of leagues there will be
			$pages = $nrofleagues / $pagelimit;
			$pages = ceil($pages);
			// If user not specified page, or page = 1, it will start showing leagues 0..pagelimit
			if(!$data['page'] or $data['page'] == 1){
				$start = '0';
				$data['page'] = '1';
			}else{
				// Figure out which leagues should be displayed
				$start = $pagelimit*$data['page']-$pagelimit;
			}
			// Recieve the list of leagues
			$request = new makeRequest($backend.'api.php?method=league&startat='.$start.'&shownr='.$pagelimit, 'GET'); // should return number of leagues
			$tmp = $request->makeRequest();
			$rbody = $tmp[0];
			$http_code = $tmp[1];
			$obj = json_decode($rbody);
			if($http_code==200){
				// Set leagues inside the table
				foreach($obj as &$value){
					$msg.=addToList($value,$backend);
				}
				$msg.="</table>";
				// Check so the previouse page cannot be 0 or negative
				if($data['page'] <= 1){
					$setback = "?page=1";
				}else{
					// Figure out priviouse page
					$bnr = $_GET['page']-1;
					$setback = '?page='.$bnr;
				}
				$msg.= '<a href="?page=1">&laquo;&laquo;</a> <a href="'.$setback.'">&laquo;</a> ';
				$i=1;
				// Create links for the other pages
				while($i <= $pages) {
					if($data['page'] == $i) {
						$msg.= '[ <a href="?page='.$i.'">'.$i.'</a> ] ';
					}else{
						$msg.= '<a href="?page='.$i.'">'.$i.'</a> ';
					}
					$i++;
				}
				if($data['page'] == $pages){
					$bigvalue=$pages;
				}else{
					$bigvalue = $data['page']+1;
				}
				$msg.= '<a href="?page='.$bigvalue.'">&raquo;</a> <a href="?page='.$pages.'">&raquo;&raquo;</a>';
			}
		}
	}
}
require 'includes/header.php';
require 'includes/topmenu.php';
echo '<div id="kaka">';
require 'includes/sidemenu.php';
echo '<div id="content">';
echo $msg;
echo '<div id="info">';
echo '</div>';
require 'includes/footer.php';
?>

</body>
</html>

